package com.sample.wanandroidx.model

import android.view.View
import android.widget.ImageView
import androidx.databinding.ObservableField
import com.bumptech.glide.Glide
import com.luck.picture.lib.PictureSelectionModel
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.listener.OnResultCallbackListener
import com.sample.library.BaseViewModel
import com.sample.library.glide.GlideEngine
import com.sample.wanandroidx.app.App
import com.sample.wanandroidx.data.User

class UserModel : BaseViewModel() {

    var user: ObservableField<User> = ObservableField(App.user)

    fun change_photo(view: View?) {
        App.instance().getCurrentVisibleActivity() ?: return
        getPictureSelectionModel().forResult(object : OnResultCallbackListener<LocalMedia?> {
            override fun onResult(result: List<LocalMedia?>) {
                (view as ImageView?)?.let {
                    Glide.with(it).load(result[0]?.path).into(it)
                }
            }

            override fun onCancel() {
            }
        })
    }

    private fun getPictureSelectionModel(): PictureSelectionModel {
        return PictureSelector.create(App.instance().getCurrentVisibleActivity()).openGallery(PictureMimeType.ofAll())
            .imageEngine(GlideEngine.createGlideEngine()).isSingleDirectReturn(true)
    }

}